package com.magenta.maxoptra.gis.distancematrix.commands;

import com.magenta.maxoptra.gis.GeographicCoordinates;
import com.magenta.maxoptra.gis.distancematrix.*;

import java.util.*;

/**
 * @author Sergey Grachev
 */
final class RemoveCoordinates extends AbstractGroupCommand {

    private final List<MatrixCoordinates> matrices;
    private boolean requireMatrixUpdating;

    RemoveCoordinates(final long groupId, final List<MatrixCoordinates> matrices) {
        super(groupId);
        this.matrices = matrices;
    }

    @Override
    public CommandType getType() {
        return CommandType.REMOVE_COORDINATES;
    }

    @Override
    public void execute(final DistanceMatrixDAO db, final DistanceMatrixServiceProxy service) {
        service.stopGroupUpdating(groupId);
        final Map<Long, Set<GeographicCoordinates>> list = new HashMap<Long, Set<GeographicCoordinates>>();
        for (final MatrixCoordinates matrix : matrices) {
            list.put(matrix.getMatrixId(), new HashSet<GeographicCoordinates>(matrix.getCoordinates()));
        }
        requireMatrixUpdating = db.removeCoordinates(groupId, list);
    }

    @Override
    public boolean isRequireGroupLock() {
        return true;
    }

    @Override
    public UpdateRequestType isRequireMatrixUpdating() {
        return requireMatrixUpdating ? UpdateRequestType.SCHEDULE : UpdateRequestType.NONE;
    }

    @Override
    public String toString() {
        return "RemoveCoordinates{" +
                "groupId=" + groupId +
                ", matrices=" + matrices +
                '}';
    }
}
